# https://fs.fun.tv/retrieve/c-e794b5e5bdb1.cl-f.os-ad.t-e585a8e983a8.y-e585a8e983a8/
# 风行网
import os
import re
import shutil

import requests

from util.headers import get_headers_init_user_agent

root = 'static/风行网/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)

url = "https://fs.fun.tv/retrieve/c-e794b5e5bdb1.cl-f.os-ad.t-e585a8e983a8.y-e585a8e983a8/"
response = requests.get(url)
# print(response.status_code)
content = response.content.decode('utf-8')
# print(content)
items = re.findall(r'<img src="//img\.funshion\.com/img/blank\.gif" _lazysrc="(.*?)" alt="(.*?)" class="lazy-alpha-start"', content)
# print(items)
for item in items:
    # print(item)
    title = item[1]
    thumb_url = item[0]
    headers = get_headers_init_user_agent()
    headers.update({
        'referer': url
    })
    thumb_response = requests.get(thumb_url, headers=headers)
    with open(f"{root}/{title}.jpg", "wb") as f:
        f.write(thumb_response.content)

# import re
#
# html_content = '''
# <a data-gtype="movie" href="/subject/27600/" title="三毛流浪记"data-id="27600">
#  <img src="//img.funshion.com/img/blank.gif" _lazysrc="https://img1.funshion.com/sdq?oid=ead19b91b4cd1d074bcec73dface67ee&w=180&h=252" alt="三毛流浪记" class="lazy-alpha-start" />
#  <i class="play-btn"></i>
#  </a>
#  '''
#
# title_pattern = r'title="([^"]+)"'
# titles = re.findall(title_pattern, html_content)
# print(titles)  # 输出: ['三毛流浪记']



# pattern = r'<img src="/img\.funshion\.com/img/blank\.gif" _lazysrc="(.*?)" alt=\'(.*?)\' class="lazy-alpha-start" />'
# items = re.findall(pattern, content)
#
# for item in items:
#     print(item)